<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use App\Rules\Hankaku;
use App\Rules\CheckTimeDiff;
use App\Rules\SpecialCharacter;
use Illuminate\Validation\ValidationException;

class ContactStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name' => ['required', 'max:50'],
            'last_name' => ['required', 'max:50'],
            'first_name_kana' => ['required', 'max:50', 'regex:/^([゠ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶヷヸヹヺ・ーヽヾヿ]+)$/u'],
            'last_name_kana' => ['required', 'max:50', 'regex:/^([゠ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶヷヸヹヺ・ーヽヾヿ]+)$/u'],
            'bod' => ['required', 'date', 'before:today'],
            'zip' => ['required', 'regex:/\d{3}-?\d{4}/'],
            'pref' => ['required', 'max:50'],
            'city' => ['required', 'max:50'],
            'town' => ['required', 'max:50'],
            'building' => ['nullable', 'max:50'],
            'email' => ['required', 'email', 'max:255'],

            'mobile' => ['required','regex:/^0[123456789]0-?\d{4}-?\d{4}$/'],

            'tel' => ['nullable',  new Hankaku, 'regex:/^0([1-9]{1}-?[1-9]\d{3}|[1-9]{2}-?\d{3}|[1-9]{2}\d{1}-?\d{2}|[1-9]{2}\d{2}-?\d{1})-?\d{4}$/'],

            'first_contact_date.date' => ['required', 'date', 'after_or_equal:today'],
            'first_contact_date.time' => ['required', 'in:am,pm', ],
            'second_contact_date.date' => ['required', 'date', 'after_or_equal:today'],
            'second_contact_date.time' => ['required', 'in:am,pm'],
            'message' => ['nullable', 'max:500'],
            'region' => ['required', 'max:100'],
            'real_estate_info' => ['nullable', 'max:50'],
            'customer_history_id' => ['required', 'exists:customer_histories,id'],
        ];
    }

    public function messages()
    {
        return [
            'first_name.required' => 'E-CONTACT-00001-REQ',
            'first_name.max' => 'E-CONTACT-00001-MAX',
            
            'last_name.required' => 'E-CONTACT-00002-REQ',
            'last_name.max' => 'E-CONTACT-00002-MAX',
            'first_name_kana.required' => 'E-CONTACT-00003-REQ',
            'first_name_kana.max' => 'E-CONTACT-00003-MAX',
            'first_name_kana.regex' => 'E-CONTACT-00003-KANA',
            'last_name_kana.required' => 'E-CONTACT-00004-REQ',
            'last_name_kana.max' => 'E-CONTACT-00004-MAX',
            'last_name_kana.regex' => 'E-CONTACT-00004-KANA',
            'bod.required' => 'E-CONTACT-00005-REQ',
            'bod.date' => 'E-CONTACT-00005-INV',
            'bod.after' => 'E-CONTACT-00005-MAX',
            'bod.before' => 'E-CONTACT-00005-MIN',
            'zip.required' => 'E-CONTACT-00006-REQ',
            'zip.regex' => 'E-CONTACT-00006-INV',
            'pref.required' => 'E-CONTACT-00007-REQ',
            'pref.max' => 'E-CONTACT-00007-MAX',
            'city.required' => 'E-CONTACT-00008-REQ',
            'city.max' => 'E-CONTACT-00008-MAX',
            'town.required' => 'E-CONTACT-00009-REQ',
            'town.max' => 'E-CONTACT-00009-MAX',
            'building.max' => 'E-CONTACT-00010-MAX',
            'email.required' => 'E-CONTACT-00011-REQ',
            'email.max' => 'E-CONTACT-00011-MAX',
            'email.email' => 'E-CONTACT-00011-INV',
            'mobile.required' => 'E-CONTACT-00012-REQ',
            'mobile.regex' => 'E-CONTACT-00012-INV',
            'tel.regex' => 'E-CONTACT-00013-INV',
            'first_contact_date.date.required' => 'E-CONTACT-00014-REQ',
            'first_contact_date.date.date' => 'E-CONTACT-00014-DAT',
            'first_contact_date.date.after_or_equal' => 'E-CONTACT-00014-PAS',
            'first_contact_date.time.required' => 'E-CONTACT-00015-REQ',
            'first_contact_date.time.in' => 'E-CONTACT-00015-TIM',
            'second_contact_date.date.required' => 'E-CONTACT-00016-REQ',
            'second_contact_date.date.date' => 'E-CONTACT-00016-DAT',
            'second_contact_date.date.after_or_equal' => 'E-CONTACT-00016-PAS',
            'second_contact_date.time.required' => 'E-CONTACT-00017-REQ',
            'second_contact_date.time.in' => 'E-CONTACT-00017-TIM',
            'message.max' => 'E-CONTACT-00018-REQ',
            'region.required' => 'E-CONTACT-00019-REQ',
            'region.max' => 'E-CONTACT-00019-MAX',
            'real_estate_info.max' => 'E-CONTACT-00020-MAX',
            'privacy_term_accepted.accepted' => 'E-CONTACT-00021-REQ',
            'temporary_member_id.required' => 'E-CONTACT-00022-REQ',
            'temporary_member_id.exists' => 'E-CONTACT-00022-INV',
            'customer_history_id.required' => 'E-CONTACT-00023-REQ',
            'customer_history_id.exists' => 'E-CONTACT-00023-INV',
        ];
    }

    public function withValidator($validator)
    {
        $validator->sometimes('first_contact_date.time', new CheckTimeDiff, function ($input) {
            return $input->first_contact_date['date'] == now()->toDateString();
        });
    }

    protected function failedValidation(Validator $validator)
    {
        $error = [
            'tel' => 'E-CONTACT-00013-NUM',
            'mobile' => 'E-CONTACT-00011-NUM',
            'first name' => 'E-CONTACT-00001-ILLEGAL',
            'last name' => 'E-CONTACT-00002-ILLEGAL',
            'first name kana' => 'E-CONTACT-00003-ILLEGAL',
            'last name kana' => 'E-CONTACT-00004-ILLEGAL',
            'pref' => 'E-CONTACT-00007-ILLEGAL',
            'city' => 'E-CONTACT-00008-ILLEGAL',
            'town' => 'E-CONTACT-00009-ILLEGAL',
            'building' => 'E-CONTACT-00010-ILLEGAL',
            'message' => 'E-CONTACT-00018-ILLEGAL',
            'region' => 'E-CONTACT-00019-ILLEGAL',
            'real estate info' => 'E-CONTACT-00020-ILLEGAL',
        ];

        foreach ($validator->errors()->messages() as $k => $v) {
            $response[$k] = isset($error[$v[0]])?  $error[$v[0]]:$v[0];
        }
        throw new HttpResponseException(response()->json([
            'code' => 422,
            'msg' => '検証に失敗しました',
            'data' => $response,
        ], 422));
    }
}
